package com.wpam.scrabble.mobile.activities;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;

import com.wpam.scrabble.mobile.GameLogic;
import com.wpam.scrabble.mobile.R;
import com.wpam.scrabble.mobile.communication.CommunicationManager;
import com.wpam.scrabble.mobile.ui.GameBoard;

public class BoardActivity extends Activity {
	
	private GameBoard gameBoard_ = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
		setContentView(R.layout.activity_board);
		gameBoard_ = (GameBoard) findViewById(R.id.the_canvas);
		gameBoard_.setParentActivity(this);
		GameLogic.getInstance().setGameBoard(gameBoard_);
		CommunicationManager.getInstance(this);
		CommunicationManager.getInstance().startDisconnectListener();
		if (getIntent().getStringArrayListExtra("PlayerNames") != null)
			GameLogic.getInstance().setupGame(getIntent().getStringArrayListExtra("PlayerNames"));
		else
			GameLogic.getInstance().setupGame();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.board_menu, menu);
	    return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		GameLogic gameLogic = GameLogic.getInstance();
		switch (item.getItemId()) {
			case R.id.okMenuItem:
				gameLogic.checkAndApplyMyMove();
				return true;
			case R.id.cancelMenuItem:
				gameLogic.cancelCurrentMove();
				return true;
			case R.id.changeLettersMenuItem:
				gameLogic.changeLetters();
				break;
			case R.id.skipTurnMenuItem:
				gameLogic.skipTurn();
				return true;
			case R.id.giveUpMenuItem:
				gameLogic.giveUp();
				return true;
			case R.id.continueMenuItem:
				gameLogic.resumeGame();
				return true;
			case R.id.scoreBoardMenuItem:
				gameLogic.showScoreBoard();
				return true;
			case R.id.endGameMenuItem:
				gameLogic.endGame();
				return true;
		}
		return super.onContextItemSelected(item);
	}


	/*
	menu.getItem(0) // ok
	menu.getItem(1) // cancel
	menu.getItem(2) // change letters
	menu.getItem(3) // skip turn
	menu.getItem(4) // give up
	menu.getItem(5) // continue
	menu.getItem(6) // score
	menu.getItem(7) // end
	*/
	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {
		
		GameLogic gameLogic = GameLogic.getInstance();
		menu.getItem(0).setVisible(!gameLogic.isGamePaused());
		menu.getItem(1).setVisible(!gameLogic.isGamePaused());
		menu.getItem(2).setVisible(!gameLogic.isGamePaused());
		menu.getItem(3).setVisible(!gameLogic.isGamePaused());
		menu.getItem(4).setVisible(!gameLogic.isGamePaused());
		menu.getItem(5).setVisible(gameLogic.amIServer() && gameLogic.getPauseReason() == GameLogic.PLAYERS_DISCONNECTED);
		menu.getItem(7).setVisible(gameLogic.isGamePaused());
		
		if (!gameLogic.isGamePaused()) {
			menu.getItem(0).setEnabled(gameLogic.isMyTurn());
			menu.getItem(1).setEnabled(gameLogic.isMyTurn());
			menu.getItem(2).setEnabled(gameLogic.isMyTurn());
			menu.getItem(3).setEnabled(gameLogic.isMyTurn());
			menu.getItem(4).setEnabled(gameLogic.isMyTurn());
		}
		return super.onMenuOpened(featureId, menu);
	}

	@Override
	public void onBackPressed() {

		final Activity thisActivity = this;
		Builder builder = new Builder(this);
		builder.setTitle(getResources().getString(R.string.quit));
		builder.setMessage(getResources().getString(R.string.do_you_want_to_quit));
		builder.setPositiveButton(getResources().getString(R.string.yes), new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				CommunicationManager.getInstance().disconnectSelfGently();
				thisActivity.finish();
				dialog.dismiss();
			}
		});
		builder.setNegativeButton(getResources().getString(R.string.no), new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
	}
	
	@Override
	protected void onDestroy() {

		CommunicationManager.getInstance().stopDisconnectListener();
		CommunicationManager.getInstance().stopNetworkActions();
		GameLogic.getInstance().closeGame();
		super.onDestroy();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		
	  super.onConfigurationChanged(newConfig);
	  gameBoard_.resize();
	}
}
